package com.kensure.shike.baobei.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import co.kensure.exception.BusinessExceptionUtil;
import co.kensure.frame.JSBaseService;
import co.kensure.mem.CollectionUtils;
import co.kensure.mem.MapUtils;

import com.kensure.basekey.BaseKeyService;
import com.kensure.shike.baobei.dao.SKBaobeiYansePicDao;
import com.kensure.shike.baobei.model.SKBaobei;
import com.kensure.shike.baobei.model.SKBaobeiYansePic;

/**
 * 规格图 服务实现类
 * 
 * @author fankd created on 2019-7-15
 * @since
 */
@Service
public class SKBaobeiYansePicService extends JSBaseService {

	@Resource
	private SKBaobeiYansePicDao dao;
	@Resource
	private BaseKeyService baseKeyService;

	public SKBaobeiYansePic selectOne(Long id) {
		return dao.selectOne(id);
	}

	public List<SKBaobeiYansePic> selectByIds(Collection<Long> ids) {
		return dao.selectByIds(ids);
	}

	public List<SKBaobeiYansePic> selectByWhere(Map<String, Object> parameters) {
		return dao.selectByWhere(parameters);
	}

	public long selectCountByWhere(Map<String, Object> parameters) {
		return dao.selectCountByWhere(parameters);
	}

	public boolean insert(SKBaobeiYansePic obj) {
		super.beforeInsert(obj);
		obj.setId(baseKeyService.getKey("sk_baobei_yansepic"));
		obj.setStatus(9);
		obj.setXiaoshou(0);
		return dao.insert(obj);
	}

	public boolean update(SKBaobeiYansePic obj) {
		return dao.update(obj);
	}

	public boolean updateByMap(Map<String, Object> params) {
		return dao.updateByMap(params);
	}

	public boolean delete(Long id) {
		return dao.delete(id);
	}

	public boolean deleteMulti(Collection<Long> ids) {
		return dao.deleteMulti(ids);
	}

	public boolean deleteByWhere(Map<String, Object> parameters) {
		return dao.deleteByWhere(parameters);
	}

	/**
	 * 获取宝贝的图片列表
	 * 
	 * @param bbid
	 * @return
	 */
	public List<SKBaobeiYansePic> getList(Long bbid) {
		Map<String, Object> parameters = MapUtils.genMap("bbid", bbid,"status",9, "orderby", "id");
		List<SKBaobeiYansePic> list = selectByWhere(parameters);
		return list;
	}

	/**
	 * 获取宝贝的图片列表
	 * 
	 * @param bbid
	 * @return
	 */
	public SKBaobeiYansePic getOneByYanse(Long bbid, String yanse) {
		Map<String, Object> parameters = MapUtils.genMap("bbid", bbid, "yanse", yanse, "orderby", "id");
		List<SKBaobeiYansePic> list = selectByWhere(parameters);
		SKBaobeiYansePic pic = null;
		if (CollectionUtils.isNotEmpty(list)) {
			pic = list.get(0);
		}
		return pic;
	}

	
	public void add(SKBaobei baobei) {
		// 删除老的
		List<SKBaobeiYansePic> oldlist = getList(baobei.getId());
		if (CollectionUtils.isNotEmpty(oldlist)) {
			for (SKBaobeiYansePic word : oldlist) {
				word.setStatus(-1);
				update(word);
			}
		}
		// 插入新的
		List<SKBaobeiYansePic> tps = baobei.getYansePics();
		List<String> yanse = new ArrayList<>();
		for (SKBaobeiYansePic tp : tps) {
			if (!yanse.contains(tp.getYanse())) {
				tp.setBbid(baobei.getId());
				SKBaobeiYansePic one = getOneByYanse(baobei.getId(), tp.getYanse());
				if (one != null) {
					tp.setId(one.getId());
					tp.setStatus(9);
					update(tp);
				} else {
					insert(tp);
				}
				yanse.add(tp.getYanse());
			} else {
				BusinessExceptionUtil.threwException(tp.getYanse() + "重复了");
			}
		}
	}

	
	/**
	 * 增加销量
	 */
	public void addNum(Long bbid, String yanse) {
		SKBaobeiYansePic one = getOneByYanse(bbid, yanse);
		if(one != null){
			one.setXiaoshou(one.getXiaoshou()+1);
			update(one);
		}
	}
}
